home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / util / libs / prooflibrary10.lha / prooflibrary / examples / create / main.c next >
C/C++ Source or Header  |  1999-01-30  |  7KB  |  245 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #include <exec/execbase.h>
  5. #include <exec/memory.h>
  6. #include <exec/types.h>
  7. #include <intuition/intuition.h>
  8. #include <intuition/intuitionbase.h>
  9. #include <cybergraphics/cybergraphics.h>
  10.  
  11. #include <proof/proof.h>
  12. #include <proof/proof_protos.h>
  13. #include <proof/proof_pragmas.h>
  14.  
  15. #include <clib/exec_protos.h>
  16. #include <clib/dos_protos.h>
  17. #include <clib/intuition_protos.h>
  18. #include <clib/cybergraphics_protos.h>
  19.  
  20. #include <pragmas/exec_pragmas.h>
  21. #include <pragmas/dos_pragmas.h>
  22. #include <pragmas/intuition_pragmas.h>
  23. #include <pragmas/cybergraphics_pragmas.h>
  24.  
  25. #include <jpeg/jpeg.h>
  26. #include <jpeg/jpeg_protos.h>
  27. #include <jpeg/jpeg_pragmas.h>
  28.  
  29. #include "scale.h"
  30.  
  31. /* Function prototypes */
  32. extern void scale_region( struct PixelRegion *srcPR, struct PixelRegion *destPR, BOOL cubic_interpolation );
  33.  
  34. /* Global variables */
  35. extern struct Library *DOSBase;
  36. struct Library *ProofBase, *JpegBase;
  37.  
  38. char *version = "$VER: ProofCreate 1.0 (19.1.99)";
  39.  
  40. void main( int argc, char **argv )
  41. {
  42.     LONG width = 80, height = 65, jpegquality = 85;
  43.     void *mempool = NULL;
  44.  
  45.     mempool = CreatePool( MEMF_PUBLIC | MEMF_CLEAR, 8000, 4000 );
  46.     ProofBase = OpenLibrary( "proof.library", NULL );
  47.     JpegBase = OpenLibrary( "jpeg.library", NULL );
  48.     if ( ProofBase != NULL && JpegBase != NULL && mempool != NULL )
  49.     {
  50.         if ( argc > 1 )
  51.         {
  52.             struct Proof *pf = NULL;
  53.  
  54.             UBYTE *rgbBuffer = NULL, *scaledBuffer = NULL;
  55.             UBYTE depth, dflags = NULL;
  56.             DOUBLE xzoom, yzoom;
  57.             LONG fileSize;
  58.             ULONG err = 0, scaledWidth, scaledHeight, finalWidth, finalHeight;
  59.             ULONG x, y, denom;
  60.             struct JPEGDecHandle *jph;
  61.             UBYTE *jstream;
  62.             ULONG jstreamsize;
  63.             struct FileInfoBlock *fib;
  64.             BPTR lock, fp;
  65.             UBYTE colourspace;
  66.             ULONG bpp;
  67.  
  68.             /* Get the size of the original image file */
  69.             fib = AllocDosObject ( DOS_FIB, NULL );
  70.  
  71.             /* Place a lock on the file */
  72.             lock = Lock( argv[1], ACCESS_READ );
  73.  
  74.             if ( lock )
  75.             {
  76.                 if ( Examine( lock, fib ) != NULL )
  77.                 {
  78.                     fileSize = fib->fib_Size;
  79.                 }
  80.                 jstreamsize = fileSize;
  81.                 jstream = AllocVec( jstreamsize, MEMF_PUBLIC | MEMF_CLEAR );
  82.                 if ( jstream != NULL )
  83.                 {
  84.                     fp = OpenFromLock( lock );
  85.                     if ( fp != NULL )
  86.                     {
  87.                         /* Spool file into memory */
  88.                         Read( fp, jstream, jstreamsize );
  89.  
  90.                         Close( fp );
  91.  
  92.                         err = AllocJPEGDecompress( &jph, JPG_SrcMemStream, jstream,
  93.                             JPG_SrcMemStreamSize, jstreamsize, TAG_DONE );
  94.                         if ( !err )
  95.                         {
  96.                             err = GetJPEGInfo( (struct JPEGHandle *)jph, JPG_Width, &x, JPG_Height, &y,
  97.                                 JPG_ColourSpace, &colourspace,
  98.                                 JPG_BytesPerPixel, &bpp,
  99.                                 JPG_ScaleNum, 1, JPG_ScaleDenom, 1, TAG_DONE );
  100.                             if ( !err )
  101.                             {
  102.                                 if ( colourspace == JPCS_GRAYSCALE )
  103.                                 {
  104.                                     dflags = PDFF_24BIT | PDFF_GREY;
  105.  
  106.                                     depth = 8;
  107.                                 }
  108.                                 else
  109.                                 {
  110.                                     dflags = PDFF_24BIT | PDFF_CHUNKY;
  111.  
  112.                                     depth = 24;
  113.                                 }
  114.  
  115.                                 err = AllocProof( &pf,
  116.                                     PFN_ImageWidth, x,
  117.                                     PFN_ImageHeight, y,
  118.                                     PFN_ImageDepth, depth,
  119.                                     PFN_ImageType, PDIV_JPEG,
  120.                                     PFN_ImageFormat, dflags,
  121.                                     PFN_ImageFileSize, fileSize,
  122.                                     PFN_CategoryOwner, 87,
  123.                                     PFN_ImageGroup, 1,
  124.                                     PFN_ImageCategory, 47,
  125.                                     PFN_ProofQuestionable, TRUE,
  126.                                     PFN_ProofAspectX, 22,
  127.                                     PFN_ProofAspectY, 22,
  128.                                     PFN_MemoryPool, mempool,
  129.                                     TAG_DONE );
  130.                                 if ( ! err )
  131.                                 {
  132.                                     /* This is divided by 2 on each pass of the loop.*/
  133.                                     denom = 16;
  134.  
  135.                                     /* Now check what scale factor is required by the jpeg loader.*/
  136.                                     do
  137.                                     {
  138.                                         denom /= 2;
  139.  
  140.                                         err = GetJPEGInfo( (struct JPEGHandle *)jph, JPG_Width, &scaledWidth, JPG_Height, &scaledHeight,
  141.                                             JPG_ScaleNum, 1, JPG_ScaleDenom, denom, TAG_DONE );
  142.  
  143.                                     } while( scaledWidth < width || scaledHeight < height );
  144.  
  145.                                     rgbBuffer = AllocVec( scaledWidth * scaledHeight * 3, MEMF_PUBLIC | MEMF_CLEAR );
  146.                                     if ( rgbBuffer )
  147.                                     {
  148.                                         err = DecompressJPEG( jph, JPG_DestRGBBuffer, rgbBuffer,
  149.                                             JPG_ScaleNum, 1, JPG_ScaleDenom, denom,
  150.                                             TAG_DONE );
  151.                                         if ( !err )
  152.                                         {
  153.                                             /* Now calculate the required zoom factor to scale the image to the exact size.*/
  154.                                             xzoom = ( double ) width / ( double ) scaledWidth;
  155.  
  156.                                             yzoom = ( double ) height / ( double ) scaledHeight;
  157.  
  158.                                             if ( xzoom < yzoom )
  159.                                             {
  160.                                                 finalWidth = xzoom * scaledWidth;
  161.                                                 finalHeight = xzoom * scaledHeight;
  162.  
  163.                                                 yzoom = ( double ) finalHeight / ( double ) scaledHeight;
  164.                                             }
  165.                                             else
  166.                                             {
  167.                                                 finalHeight = yzoom * scaledHeight;
  168.                                                 finalWidth = yzoom * scaledWidth;
  169.  
  170.                                                 xzoom = ( double ) finalWidth / ( double ) scaledWidth;
  171.                                             }
  172.  
  173.                                             scaledBuffer = AllocVec( finalWidth * finalHeight * bpp, MEMF_PUBLIC | MEMF_CLEAR );
  174.                                             if ( scaledBuffer != NULL )
  175.                                             {
  176.                                                 struct PixelRegion src, dest;
  177.  
  178.                                                 src.buf = rgbBuffer;
  179.                                                 src.w = scaledWidth;
  180.                                                 src.h = scaledHeight;
  181.                                                 src.bytes = bpp;
  182.  
  183.                                                 dest.buf = scaledBuffer;
  184.                                                 dest.w = finalWidth;
  185.                                                 dest.h = finalHeight;
  186.                                                 dest.bytes = bpp;
  187.  
  188.                                                 scale_region( &src, &dest, TRUE );
  189.  
  190.                                                 err = SaveProof( &pf,
  191.                                                     PFN_ImagePathName, argv[1],
  192.                                                     PFN_ProofPath, "RAM:",
  193.                                                     PFN_ProofWidth, finalWidth,
  194.                                                     PFN_ProofHeight, finalHeight,
  195.                                                     PFN_ProofBodyType, PHBV_JFIF,
  196.                                                     PFN_RGBTripletBuffer, scaledBuffer,
  197.                                                     PFN_JFIFQuality, jpegquality,
  198.                                                     PFN_MemoryPool, mempool,
  199.                                                     TAG_DONE );
  200.                                                 if ( err == PFERR_NONE )
  201.                                                 {
  202.                                                     printf("proof saved\n");
  203.                                                 }
  204.                                                 else printf("proof error 2=%d\n",err);
  205.  
  206.                                                 FreeVec( scaledBuffer );
  207.                                             }
  208.                                             else printf("cant allocate scaled buffer\n");
  209.                                         }
  210.                                         else printf("jpeg error 3=%d\n",err);
  211.  
  212.                                         FreeVec( rgbBuffer );
  213.                                     }
  214.                                     else printf("cant allocate load jpeg buffer");
  215.  
  216.                                     FreeProof( &pf, PFN_MemoryPool, mempool, TAG_DONE );
  217.                                 }
  218.                                 else printf("proof error 1=%d\n",err);
  219.                             }
  220.                             else printf("jpeg error 2=%d\n",err);
  221.  
  222.                             FreeJPEGDecompress( jph );
  223.                         }
  224.                         else printf("jpeg error 1=%d\n",err);
  225.                     }
  226.                     else UnLock( lock );
  227.  
  228.                     FreeVec( jstream );
  229.                 }
  230.                 else printf("cant allocate source file memory");
  231.             }
  232.             else printf("cant lock file");
  233.  
  234.  
  235.             if ( fib ) FreeDosObject( DOS_FIB, fib );
  236.         }
  237.     }
  238.  
  239.     if ( ProofBase != NULL ) CloseLibrary( ProofBase );
  240.     if ( JpegBase != NULL ) CloseLibrary( JpegBase );
  241.  
  242.     if ( mempool != NULL ) DeletePool( mempool );
  243.     else printf("cant allocate memory pool\n");
  244. }
  245.